' Strict
SuperStrict

' Modules
Import BRL.Map
Import BRL.Audio
Import BRL.FileSystem

' File formats
Import BRL.WAVLoader
Import BRL.OGGLoader

' Files
Import "Global.bmx"
Import "TLog.bmx"

' TSoundManager
Type TSoundManager
	Field channels:TMap
	Field logger:TLog
	
	' Init
	Method Init()
		Self.channels = CreateMap()
		
		?Debug
			Self.logger = TLog.Create(StandardIOStream)
		?Not Debug
			Self.logger = TLog.Create(WriteFile(FS_ROOT + "logs/audio.txt"))
		?
		
		Self.logger.Write("Sound manager initialized")
	End Method
	
	' AddChannel
	Method AddChannel:TSoundChannel(name:String) 
		Self.logger.Write("Creating sound channel '" + name + "'")
		Local chan:TSoundChannel = TSoundChannel.Create(name, Self.logger)
		Self.channels.Insert(name, chan) 
		Return chan
	End Method
	
	' GetChannel
	Method GetChannel:TSoundChannel(name:String)
		Return TSoundChannel(Self.channels.ValueForKey(name))
	End Method
	
	' Create
	Function Create:TSoundManager() 
		Local mgr:TSoundManager = New TSoundManager
		mgr.Init()
		Return mgr
	End Function
End Type

' TSoundChannel
Type TSoundChannel
	Field name:String
	Field sounds:TMap
	'Field channel:TChannel
	Field logger:TLog
	
	' Init
	Method Init(nName:String, nLogger:TLog) 
		Self.name = nName
		Self.sounds = CreateMap()
		'Self.channel = AllocChannel() 
		Self.logger = nLogger
	End Method
	
	' AddSound
	Method AddSound(name:String, file:String) 
		Self.WriteLog("Loading sound '" + name + "': " + file)
		
		Local sound:TSound = LoadSound(file) 
		If sound <> Null
			Self.WriteLog("Successfully loaded sound '" + name + "'")
			Self.sounds.Insert(name, sound)
		Else
			Self.WriteLog("Failed loading sound '" + name + "'")
		EndIf
	End Method
	
	' AddSoundDirectory
	Method AddSoundDirectory(dir:String) 
		Self.WriteLog("Loading directory: " + dir)
		
		Local ext:String
		Local files:String[] = LoadDir(dir) 
		For Local file:String = EachIn files
			ext = ExtractExt(file).ToLower()
			If ext = "wav" Or ext = "ogg"
				Self.AddSound(StripAll(file), dir + file) 
			EndIf
		Next
	End Method
	
	' Play
	Method Play(name:String) 
		Local sound:TSound = TSound(Self.sounds.ValueForKey(name)) 
		
		If sound <> Null
			Self.WriteLog("Playing sound '" + name + "'")
			sound.Play()
		Else
			Self.WriteLog("Failed playing sound '" + name + "'")
		EndIf
	End Method
	
	' Cue
	Method Cue(name:String) 
		Local sound:TSound = TSound(Self.sounds.ValueForKey(name)) 
		
		If sound <> Null
			sound.Cue()
		EndIf
	End Method
	
	' WriteLog
	Method WriteLog(msg:String) 
		Self.logger.Write("[" + Self.name + "] " + msg)
	End Method
	
	Rem
	' Stop
	Method Stop() 
		Self.channel.Stop()
	End Method
	
	' Resume
	Method Resume() 
		Self.channel.SetPaused(False)
	End Method
	
	' Pause
	Method Pause() 
		Self.channel.SetPaused(True)
	End Method
	
	' SetPaused
	Method SetPaused(paused:Int) 
		Self.channel.SetPaused(paused)
	End Method
	
	' SetVolume
	Method SetVolume(volume:Float) 
		Self.channel.SetVolume(volume)
	End Method
	
	' SetPan
	Method SetPan(pan:Float) 
		Self.channel.SetPan(pan)
	End Method
	
	' SetDepth
	Method SetDepth(depth:Float) 
		Self.channel.SetDepth(depth)
	End Method
	
	' SetRate
	Method SetRate(rate:Float) 
		Self.channel.SetRate(rate)
	End Method
	
	' IsPlaying
	Method IsPlaying:Int() 
		Return Self.channel.Playing()
	End Method
	End Rem
	
	' Create
	Function Create:TSoundChannel(nName:String, nLogger:TLog) 
		Local chan:TSoundChannel = New TSoundChannel
		chan.Init(nName, nLogger)
		Return chan
	End Function
End Type
